// 构造器模式
function Person() {
  this.name;
  this.age;
  this.getName = function () {
    return this.name;
  };
  this.setName = function (name) {
    this.name = name;
  };
  this.getAge = function () {
    return this.age;
  };
  this.setAge = function (age) {
    this.age = age;
  };
}

// 重写toString方法
Person.prototype.toString = function () {
  return `{name:${this.name},age:${this.age}}`;
};

const p = new Person();
p.setAge(18);
p.setName("小李");
Object.defineProperty(p, "sex", {
  get: () => {
    console.log(this);
    return this.sex + "12";
  },
  set: (val) => {
    this.sex = val;
  },
});

p.sex = "男";
console.log(p.sex);
// Object.observe(p, (info) => {
//   console.log(info);
// });

// 实现一个new
function myNew(fn, ...args) {
  const obj = Object.create(fn.prototype);
  const ret = fn.apply(obj, args);
  return ret instanceof Object ? ret : obj;
}
